package com.jic.member.impl;

import com.jic.common.base.vo.PageResult;
import com.jic.common.base.vo.RestResult;
import com.jic.manager.api.rest.UserSysDeptApi;
import com.jic.manager.request.MestorePageRequest;
import com.jic.manager.response.MestoreResponse;

import com.jic.member.request.MememberActionRequest;
import com.jic.member.service.MenStoreMemberService;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.collections.CollectionUtils;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.Iterator;

@Service
@Slf4j
public class MenStoreMemberServiceImpl implements MenStoreMemberService {

    @Autowired
    private UserSysDeptApi storeApi;


    /**
     * 查询店铺会员列表
     *
     * @param form
     * @return
     */
    @Override
    public RestResult<PageResult<MestoreResponse>> queryPage(MestorePageRequest form) {
        //查询所有店铺信息
        MestorePageRequest storeParam = new MestorePageRequest();
        BeanUtils.copyProperties(form, storeParam);
        //查询所有数据
        storeParam.setReviewStatus("0");
        if (form.getStart() == 1) {
            storeParam.setLimit(1000);//todo  替换为不分页查询
        }
        final RestResult<PageResult<MestoreResponse>> apiResult = storeApi.getStorePage(storeParam);
        RestResult<PageResult<MestoreResponse>> result = new RestResult<PageResult<MestoreResponse>>();
        result.setData(new PageResult<MestoreResponse>());

        result.getData().setRows(new ArrayList<MestoreResponse>());
        result.getData().setTotal(apiResult.getData().getTotal());
        if (CollectionUtils.isNotEmpty(apiResult.getData().getRows())) {
            if (apiResult.getData().getRows().size() > 0) {
                for (MestoreResponse row : apiResult.getData().getRows()) {
                    MememberActionRequest param = new MememberActionRequest();
                    param.setMemberUserId(row.getId());
                    param.setStoreMember(1);

                }
                Iterator<MestoreResponse> iterator = result.getData().getRows().iterator();

                while (iterator.hasNext()) {
                    MestoreResponse next = iterator.next();
                    long status = next.getReviewStatus();
                    if (StringUtils.isNotEmpty(form.getReviewStatus()) && form.getReviewStatus().equals("0")) {//启用
                        if (status == 1) {
                            iterator.remove();
                            long total = result.getData().getTotal();
                            System.err.println(total);
                            result.getData().setTotal(total -= 1);
                            if (result.getData().getTotal() == 0) {
                                return new RestResult<>();
                            }
                        }
                    }
                    if (StringUtils.isNotEmpty(form.getReviewStatus()) && form.getReviewStatus().equals("1")) {//停用
                        if (status == 0) {
                            iterator.remove();
                            long total = result.getData().getTotal();
                            System.err.println(total);
                            result.getData().setTotal(total -= 1);
                            if (result.getData().getTotal() == 0) {
                                return new RestResult<>();
                            }
                        }
                    }
                }
                if (form.getLimit() <= result.getData().getRows().size()) {
                    result.getData().setRows(result.getData().getRows().subList(0, form.getLimit() - 1));
                }

                return result;
            }
        }

        return result;
    }

}
